﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;

namespace QuanLyThuNgan
{
    public partial class frm_DM_GiaDichVu : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_GiaDichVu()
        {
            InitializeComponent();
        }

        #region "enum"
        enum DatasetStatus
        {
            Add=1,
            Edit=2,
            None=0
        }

        #endregion

        string sTenSearch = "";
        string sColSearch;
        public static string sIDSearch=""; 
        public static string sTenValueSearch="";
        string sTenTam, sColTam;
        int iRowIndex;

        private DatasetStatus m_Status = DatasetStatus.None;
        DMLoaiDVYTCTL loaiDVYTCtl = new DMLoaiDVYTCTL();
        GiaDichVuCTL giaDVCtl = new GiaDichVuCTL();
        GiaDichVuDTO giaDVDto = new GiaDichVuDTO();

        private void frm_DM_GiaDichVu_Load(object sender, EventArgs e)
        {
            //load gridlookupedit ten dich vu-----------------------
            DataTable dtLoaidvyt = loaiDVYTCtl.m_SelectTenLoaiDVYT();
            ClassHelp.m_gridLookUpEdit(dtLoaidvyt, gdLoaiDVYT, "TenLoaiDichVuYTe", "IDLoaiDichVuYTe");
            //invisible IDLoaiDichVuYT--------------------------------------
           // gdLoaiDVYT.Properties.View.Columns["IDLoaiDichVuYTe"].Visible = false;
            //load danh muc gia dich vu----------------------------
            DataTable dtloadGiaDV = giaDVCtl.m_SelectAllGiaDichVu();
            gridControlGiaDVu.DataSource = dtloadGiaDV;

            //set button condition------------------------
            setReadonly();
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
        }

        public string _dvid="";
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (m_checkSave() == true)
            {

                giaDVDto.Smahieu = txtMaHieu.Text.ToString().Trim();
                giaDVDto.Sdvt = Uit.it_Font.UnicodeToVNI(txtDonViTinh.Text.ToString().Trim());
                giaDVDto.Stendvu = Uit.it_Font.UnicodeToVNI(txtTenGiaDichVu.Text.ToString().Trim());
                giaDVDto.Igiabieu = Uit.it_Parse.ToInteger(txtGiaBieu.Text.ToString().Trim());
                giaDVDto.Igiabhyt = Uit.it_Parse.ToInteger(txtGiaBHYT.Text.ToString().Trim());
                giaDVDto.Bstopsd = Uit.it_Parse.ToBoolean(chkStop.Checked);
                giaDVDto.IIDLoaiDichVuYTe = Uit.it_Parse.ToInteger(gdLoaiDVYT.EditValue.ToString());
                giaDVDto.STenGiaDVu = txtTenGiaDichVu.Text.ToString().Trim();
                giaDVDto.SDonViTinh = txtDonViTinh.Text.ToString().Trim();
                giaDVDto.SMaBHYT = txtMaBHYT.Text.ToString().Trim();
                DataRow dr = gvGiaDVu.GetDataRow(gvGiaDVu.FocusedRowHandle);
                if (m_Status == DatasetStatus.Add)
                {
                    giaDVCtl.m_InsertGiaDichVu(giaDVDto);
                    XtraMessageBox.Show("Thêm thành công");
                }
                else
                {
                    if (m_Status == DatasetStatus.Edit)
                    {
                        giaDVDto.Idvuid = Uit.it_Parse.ToInteger(dr["dvuid"].ToString());
                        giaDVCtl.m_UpdateGiaDichVu(giaDVDto);
                        XtraMessageBox.Show("Cập nhật thành công");
                    }
                }

            }
            else
            {
                XtraMessageBox.Show("Phải hòan thành thông tin nhập liệu");
                return;
            }

            //XtraMessageBox.Show("Hoàn thành thông tin");

            DataTable dtloadGiaDV = giaDVCtl.m_SelectAllGiaDichVu();
            gridControlGiaDVu.DataSource = dtloadGiaDV;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            setNull();
            setReadonly();

            //reset load du lieu----------------------
            //gvGiaDVu.ActiveFilterEnabled = false;
            //gvGiaDVu.ClearColumnsFilter();
            //gvGiaDVu.OptionsFilter.Reset();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            m_Status = DatasetStatus.Edit;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
            setReadonlyFalse();
        }

        #region "Validate value"

        private void txtMaHieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.KeyChar = char.Parse(e.KeyChar.ToString().ToUpper());
        }

        private void txtTenGiaDichVu_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.KeyChar = char.Parse(e.KeyChar.ToString().ToUpper());
        }

        private void txtDonViTinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.KeyChar = char.Parse(e.KeyChar.ToString().ToUpper());
        }

        private void txtMaBHYT_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.KeyChar = char.Parse(e.KeyChar.ToString().ToUpper());
        }
        //kiem tra gia tri nhap la so--------------------------------------------------
        private void txtGiaBieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                XtraMessageBox.Show("Chỉ nhập kí tự số và không được nhập ký tự chữ cái");
                e.Handled = true;
            }

        }

        private void txtGiaBHYT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                XtraMessageBox.Show("Chỉ nhập kí tự số và không được nhập ký tự chữ cái");
                e.Handled = true;
            }
            
        }

        //kiem tra gia tri trung khi nhap--------------------------------------------------
        private void txtTenGiaDichVu_Leave(object sender, EventArgs e)
        {
            //if (txtTenGiaDichVu.Text.Length >= 1)
            //    txtTenGiaDichVu.Text = txtTenGiaDichVu.Text.Substring(0, 1).ToUpper() + txtTenGiaDichVu.Text.Substring(1);
            
            //for (int i = 0; i < gvGiaDVu.RowCount; i++)
            //{
            //    DataRow dr = gvGiaDVu.GetDataRow(i);
            //    if (dr["TenGiaDVu"].ToString().ToUpper().Trim() == txtTenGiaDichVu.Text.ToString().Trim() || dr["TenGiaDVu"].ToString().ToLower().Trim() == txtTenGiaDichVu.Text.ToString().Trim())
            //    {

            //        XtraMessageBox.Show("Trùng tên giá dịch vụ");
            //        dxErrorProvider1.SetError(txtTenGiaDichVu, "Trùng tên giá dịch vụ");
            //        txtTenGiaDichVu.Focus();
            //        return ;
            //    }
            //    else
            //        if (txtTenGiaDichVu.Text.ToString() == "")
            //        {
            //            break;
            //        }
                   
            //}
        }

        private void txtMaHieu_Leave(object sender, EventArgs e)
        {
            //for (int i = 0; i < gvGiaDVu.RowCount; i++)
            //{
            //    DataRow dr = gvGiaDVu.GetDataRow(i);
            //    if (dr["mahieu"].ToString().ToUpper().Trim() == txtMaHieu.Text.ToString().Trim() || dr["mahieu"].ToString().ToLower().Trim() == txtMaHieu.Text.ToString().Trim())
            //    {

            //        XtraMessageBox.Show("Trùng mã hiệu");
            //        dxErrorProvider1.SetError(txtMaHieu, "Trùng mã hiệu");
            //        txtMaHieu.Focus();
            //        return ;
            //    }
            //    else
            //        if (txtMaHieu.Text.ToString() == "")
            //        {
            //            break;
            //        }
            //}
        }

        private void txtMaBHYT_Leave(object sender, EventArgs e)
        {
            //for (int i = 0; i < gvGiaDVu.RowCount; i++)
            //{
            //    DataRow dr = gvGiaDVu.GetDataRow(i);
            //    if (dr["MaBHYT"].ToString().ToUpper().Trim() == txtMaBHYT.Text.ToString().Trim() || dr["MaBHYT"].ToString().ToLower().Trim() == txtMaBHYT.Text.ToString().Trim())
            //    {

            //        XtraMessageBox.Show("Trùng mã BHYT");
            //        txtMaBHYT.Focus();
            //        return;
            //    }
            //    else
            //        if (txtMaBHYT.Text.ToString() == "")
            //        {
            //            break;
            //        }
            //}
        }


        //tim ten can search---------------------------------------------
        private void gvGiaDVu_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }
        //set ten can tim----------------------------------------------------------------
        private void gvGiaDVu_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvGiaDVu;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));

        }

        //tab to next position and show gridlookupedit value---------------------
        private void gdLoaiDVYT_Enter(object sender, EventArgs e)
        {
            SendKeys.Send("{F4}");
        }

        //search value in gridlookupedit---------------------------------------
        private void gdLoaiDVYT_EditValueChanged(object sender, EventArgs e)
        {
            if (gdLoaiDVYT.EditValue != null)
            {
                sIDSearch = gdLoaiDVYT.EditValue.ToString();
                sTenValueSearch = gdLoaiDVYT.Text;
                dxErrorProvider1.SetError(gdLoaiDVYT, "");
            }

        }


        private void gridLookUpEdit1View_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenTam = e.Value.ToString();
            sColTam = e.Column.FieldName;
        }

        private void gridLookUpEdit1View_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gdLoaiDVYT.Properties.View;
            view.ActiveFilter.Add(view.Columns[sColTam],
              new ColumnFilterInfo("[" + sColTam + "] Like '%" + sTenTam + "%'", ""));
        }

        private void txtMaHieu_EditValueChanged(object sender, EventArgs e)
        {
            if (txtMaHieu.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtMaHieu, "");
            }
        }

        private void txtTenGiaDichVu_EditValueChanged(object sender, EventArgs e)
        {
            if (txtTenGiaDichVu.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtTenGiaDichVu, "");
            }
        }

        private void txtDonViTinh_EditValueChanged(object sender, EventArgs e)
        {
            if (txtDonViTinh.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtDonViTinh, "");
            }
        }

        private void txtGiaBieu_EditValueChanged(object sender, EventArgs e)
        {
            if (txtGiaBieu.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtGiaBieu, "");
            }
        }

        private void txtGiaBHYT_EditValueChanged(object sender, EventArgs e)
        {
            if (txtGiaBHYT.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtGiaBHYT, "");
            }
        }

        #endregion


        bool m_checkSave()
        {
            bool bCheckValue = true;
            if (txtMaHieu.Text.ToString().Trim() == "")
            {
                dxErrorProvider1.SetError(txtMaHieu, "Phải nhập mã hiệu");
                txtMaHieu.Focus();
                bCheckValue = false;
            }

            if (txtTenGiaDichVu.Text.ToString().Trim() == "")
            {
                dxErrorProvider1.SetError(txtTenGiaDichVu, "Phải nhập tên giá dịch vụ");
                txtTenGiaDichVu.Focus();
                bCheckValue = false;
            }

            if (txtDonViTinh.Text.ToString().Trim() == "")
            {
                dxErrorProvider1.SetError(txtDonViTinh, "Phải nhập đơn vị tính");
                txtDonViTinh.Focus();
                bCheckValue = false;
            }

            if (txtGiaBieu.Text.ToString().Trim() == "")
            {
                dxErrorProvider1.SetError(txtGiaBieu, "Phải nhập giá biểu");
                txtGiaBieu.Focus();
                bCheckValue = false;
            }

            if (txtGiaBHYT.Text.ToString().Trim() == "")
            {
                dxErrorProvider1.SetError(txtGiaBHYT, "Phải nhập giá BHYT");
                txtGiaBHYT.Focus();
                bCheckValue = false;
            }

            if (Uit.it_Parse.ToString(gdLoaiDVYT.EditValue) == "-1" || Uit.it_Parse.ToString(gdLoaiDVYT.EditValue) == "")
            {
                dxErrorProvider1.SetError(gdLoaiDVYT, "Phải nhập loại DVYT");
                //gdLoaiDVYT.Focus();
                bCheckValue = false;
            }

            //-------------------------------------------------------------------------------
            //kiem tra gia tri ma hieu bi trung--------------------
            for (int i = 0; i < gvGiaDVu.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow dr = gvGiaDVu.GetDataRow(i);
                    if (dr["mahieu"].ToString().ToUpper().Trim() == txtMaHieu.Text.ToString().ToUpper().Trim() )
                    {

                        //XtraMessageBox.Show("Trùng mã hiệu");
                        dxErrorProvider1.SetError(txtMaHieu, "Trùng mã hiệu");
                        txtMaHieu.Focus();
                        bCheckValue = false;
                    }
                    else
                        if (txtMaHieu.Text.ToString() == "")
                        {
                            break;
                        }
                }
                
            }

            //xet gia tri ten gia dich vu bi trung-------------------------
            for (int i = 0; i < gvGiaDVu.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow dr = gvGiaDVu.GetDataRow(i);
                    if (dr["TenGiaDVu"].ToString().ToUpper().Trim() == txtTenGiaDichVu.Text.ToString().ToUpper().Trim())
                    {

                        //XtraMessageBox.Show("Trùng tên giá dịch vụ");
                        dxErrorProvider1.SetError(txtTenGiaDichVu, "Trùng tên giá dịch vụ");
                        txtTenGiaDichVu.Focus();
                        bCheckValue=false;
                    }
                    else
                        if (txtTenGiaDichVu.Text.ToString() == "")
                        {
                            break;
                        }

                }
                
            }
            return bCheckValue;
        }

        //int idv = 0;
        private void gvGiaDVu_DoubleClick(object sender, EventArgs e)
        {

            gridControlGiaDVu.Focus();
            //DataRow drdv = gvGiaDVu.GetDataRow(idv);
            DataRow drdv = gvGiaDVu.GetDataRow(gvGiaDVu.FocusedRowHandle);
                //if (idv>=0)
                //{
                    txtMaHieu.Text = drdv["mahieu"].ToString();
                    txtTenGiaDichVu.Text = drdv["TenGiaDVu"].ToString();
                    txtDonViTinh.Text = drdv["DonViTinh"].ToString();
                    txtGiaBieu.Text = drdv["giabieu"].ToString();
                    txtMaBHYT.Text = drdv["MaBHYT"].ToString();
                    txtGiaBHYT.Text = drdv["giabhyt"].ToString();
                    gdLoaiDVYT.EditValue = drdv["IDLoaiDichVuYTe"].ToString();
                    chkStop.Checked = Uit.it_Parse.ToBoolean(drdv["stopsd"].ToString());

                //}
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvGiaDVu.GetDataRow(gvGiaDVu.FocusedRowHandle);
            if (dr != null)
            {
                giaDVDto.Idvuid = Uit.it_Parse.ToInteger(dr["dvuid"].ToString());
                giaDVCtl.m_DeleteGiaDichVu(giaDVDto);

                DataTable dtloadGiaDV = giaDVCtl.m_SelectAllGiaDichVu();
                gridControlGiaDVu.DataSource = dtloadGiaDV;
            }
            XtraMessageBox.Show("Xóa thành công");
            setNull();

            //reset load du lieu----------------------
            gvGiaDVu.ActiveFilterEnabled = false;
            gvGiaDVu.ClearColumnsFilter();
            gvGiaDVu.OptionsFilter.Reset();
        }

        private void gvGiaDVu_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            DataRow dr = gvGiaDVu.GetDataRow(gvGiaDVu.FocusedRowHandle);
            if (dr != null)
            {
                giaDVDto.Idvuid = Uit.it_Parse.ToInteger(dr["dvuid"].ToString());
                giaDVCtl.m_DeleteGiaDichVu(giaDVDto);

                DataTable dtloadGiaDV = giaDVCtl.m_SelectAllGiaDichVu();
                gridControlGiaDVu.DataSource = dtloadGiaDV;
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            m_Status = DatasetStatus.Add;
            btnSave.Enabled = true;
            btnInsert.Enabled = false;
            setReadonlyFalse();
            setNull();

            //reset load du lieu----------------------
            gvGiaDVu.ActiveFilterEnabled = false;
            gvGiaDVu.ClearColumnsFilter();
            gvGiaDVu.OptionsFilter.Reset();
        }

        #region "Condition value"
        void setNull()
        {

            txtMaHieu.Text ="";
            txtTenGiaDichVu.Text=""; 
            txtDonViTinh.Text=""; 
            txtGiaBieu.Text="";
            txtMaBHYT.Text="";
            txtGiaBHYT.Text="";
            gdLoaiDVYT.EditValue=-1;
            chkStop.Checked = false;
        }

        void setReadonly()
        {
            txtMaHieu.Properties.ReadOnly = true;
            txtTenGiaDichVu.Properties.ReadOnly = true;
            txtDonViTinh.Properties.ReadOnly = true;
            txtGiaBieu.Properties.ReadOnly = true;
            txtMaBHYT.Properties.ReadOnly = true;
            txtGiaBHYT.Properties.ReadOnly = true;
            gdLoaiDVYT.Properties.ReadOnly=true ;
            chkStop.Properties.ReadOnly = true;
        }

        void setReadonlyFalse()
        {
            txtMaHieu.Properties.ReadOnly = false;
            txtTenGiaDichVu.Properties.ReadOnly = false;
            txtDonViTinh.Properties.ReadOnly = false;
            txtGiaBieu.Properties.ReadOnly = false;
            txtMaBHYT.Properties.ReadOnly = false;
            txtGiaBHYT.Properties.ReadOnly = false;
            gdLoaiDVYT.Properties.ReadOnly = false;
            chkStop.Properties.ReadOnly=false;
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            setNull();
            DataTable dtloadGiaDV = giaDVCtl.m_SelectAllGiaDichVu();
            gridControlGiaDVu.DataSource = dtloadGiaDV;

            //reset load du lieu----------------------
            gvGiaDVu.ActiveFilterEnabled = false;
            gvGiaDVu.ClearColumnsFilter();
            gvGiaDVu.OptionsFilter.Reset();
        }

        private void gvGiaDVu_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            //lay gia tri tai vi tri focus ---------------
            iRowIndex = e.FocusedRowHandle;

            //lay gia tri tai row khi click---------------
            txtMaHieu.EditValue = (sender as GridView).GetFocusedRowCellValue("mahieu");
            txtTenGiaDichVu.EditValue = (sender as GridView).GetFocusedRowCellValue("TenGiaDVu");
            txtDonViTinh.EditValue = (sender as GridView).GetFocusedRowCellValue("DonViTinh");
            txtGiaBieu.EditValue = (sender as GridView).GetFocusedRowCellValue("giabieu");
            txtMaBHYT.EditValue = (sender as GridView).GetFocusedRowCellValue("MaBHYT"); ;
            txtGiaBHYT.EditValue = (sender as GridView).GetFocusedRowCellValue("giabhyt");
            gdLoaiDVYT.EditValue = (sender as GridView).GetFocusedRowCellValue("IDLoaiDichVuYTe"); ;
            chkStop.EditValue = (sender as GridView).GetFocusedRowCellValue("stopsd");
        }  
        
    }
}